package com.castle.fortress.admin.flowable.delegate;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.castle.fortress.admin.oa.entity.HolidayTypeEntity;
import com.castle.fortress.admin.oa.entity.UserHolidaySettingEntity;
import com.castle.fortress.admin.oa.service.HolidayTypeService;
import com.castle.fortress.admin.oa.service.UserHolidaySettingService;
import com.castle.fortress.common.enums.YesNoEnum;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 假勤组件结束
 * @author castle
 */
@Service
public class FortressHolidayCategaryEndListener implements ExecutionListener {
    @Autowired
    private UserHolidaySettingService userHolidaySettingService;
    @Autowired
    private HolidayTypeService holidayTypeService;


    @Override
    public void notify(DelegateExecution delegateExecution) {
        final Map<String, Object> variables = delegateExecution.getVariables();
        if ("end".equals(delegateExecution.getEventName()) && YesNoEnum.NO.getCode().toString().equals(variables.get("approveStatus"))){
            //判断是同意还是拒绝  同意不作操作 拒绝 将扣减的假期余额加回去
            //从 variables 取出流程发起人 / 请假类型 / 请假时长
            final Object castle_flowable_start_userid = variables.get("_castle_flowable_start_userid");
            final Object holidayTypeId = variables.get("leaveType");
            final Object holiday_total_days = variables.get("leaveDuration");
            // 如果请假类型 以及时长 不为空
            if (holidayTypeId!=null && holiday_total_days !=null && StrUtil.isNotEmpty(holidayTypeId.toString()) && StrUtil.isNotEmpty(holiday_total_days.toString())){
                // 获取请假类型信息
                final HolidayTypeEntity holidayTypeEntity = holidayTypeService.getById(holidayTypeId.toString());
                // 有限制时长的请假类型 把时间加回去  没有限制时长的 无需增加假期余额
                if (YesNoEnum.YES.getCode().equals(holidayTypeEntity.getIsLimit())) {
                    // 获取用户该类型假期设置
                    final QueryWrapper<UserHolidaySettingEntity> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("user_id", castle_flowable_start_userid.toString());
                    queryWrapper.eq("holiday_type_id", holidayTypeEntity.getId());
                    queryWrapper.last("limit 1");
                    final UserHolidaySettingEntity one = userHolidaySettingService.getOne(queryWrapper);
                    // 剩余天数
                    final BigDecimal surplusAmount = one.getSurplusAmount();
                    final double sAmount = surplusAmount.doubleValue();
                    // 请假天数
                    final double addition = Double.parseDouble(holiday_total_days.toString());
                    //然后退回余额
                    //如果退回的余额相加超过了 当前设置的额度 那么就按照当前设置的额度 而不是 数量累加 超出当前额度的限额
                    if ((sAmount + addition) >= holidayTypeEntity.getAmount()) {
                        one.setSurplusAmount(new BigDecimal(holidayTypeEntity.getAmount()));
                        one.setTotalAmount(new BigDecimal(holidayTypeEntity.getAmount()));
                    } else {
                        //小于等于的话直接加上去即可
                        one.setSurplusAmount(new BigDecimal(sAmount + addition));
                    }
                    userHolidaySettingService.updateById(one);
                }
            }
        }
    }
}
